package com.workflow.service.impl;

import java.util.List;

import com.base.bean.Cardinfo;
import com.common.bean.Datadict;
import com.common.util.AbstractManager;
import com.common.util.StringToken;
import com.workflow.bean.Inputcard;
import com.workflow.service.InputcardService;

public class InputcardServiceImpl extends AbstractManager implements
		InputcardService {

	public void addInputcard(String cardno, String intdetailno) {
		Inputcard inputcard = new Inputcard();
		inputcard.setCardno(cardno);
		inputcard.setIntdetailno(intdetailno);
		
		this.getHibernateTemplate().saveOrUpdate(inputcard);
		Cardinfo cardinfo = null;
		cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(intdetailno);
		this.getHibernateTemplate().update(cardinfo);
	}

	@SuppressWarnings("unchecked")
	public List<Inputcard> findAllInputcard() {
		String hql = "from Inputcard order by intdetailno,cardno";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Inputcard> findAllInputcardByIntdetailno(String intdetailno) {
		String hql = "from Inputcard card where card.intdetailno = ? order by card.cardno";
		return this.getHibernateTemplate().find(hql,intdetailno);
	}

	public Inputcard findInputcard(String cardno) {
		return (Inputcard) this.getHibernateTemplate().load(Inputcard.class, cardno);
	}

	public void removeInputcard(String cardno) {
		Inputcard inputcard = null;
		inputcard = (Inputcard) this.getHibernateTemplate().load(Inputcard.class, cardno);
		if(inputcard != null) {
			this.getHibernateTemplate().delete(inputcard);
			
			Cardinfo cardinfo = null;
			cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
			Datadict datadictByStatus = new Datadict();
//			置卡状态为初始 状态编号 71l
			datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_INIT);
			cardinfo.setDatadictByStatus(datadictByStatus);
			cardinfo.setBelongto("");//取消所属状态，临时
			this.getHibernateTemplate().update(cardinfo);
		}
	}

	public void updateInputcard(String cardno, String intdetailno) {
		Inputcard inputcard = new Inputcard();
		inputcard.setCardno(cardno);
		inputcard.setIntdetailno(intdetailno);
		
		this.getHibernateTemplate().saveOrUpdate(inputcard);
		Cardinfo cardinfo = null;
		cardinfo = (Cardinfo) this.getHibernateTemplate().load(Cardinfo.class, cardno);
		Datadict datadictByStatus = new Datadict();
		//置卡状态为锁定状态  75l
		datadictByStatus.setTypeid(StringToken.DICT_CARDSTATUS_LOCK);
		cardinfo.setDatadictByStatus(datadictByStatus);
		cardinfo.setBelongto(intdetailno);
		this.getHibernateTemplate().update(cardinfo);
	}

}
